package com.demo2do.boyle.backend.web.controller.task;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.service.advertisement.TaskService;
import com.demo2do.boyle.common.entity.customer.social.SocialType;
import com.demo2do.boyle.common.entity.task.Task;
import com.demo2do.boyle.common.entity.task.TaskStatus;
import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.utils.StringUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;

/**
 * 
 * @author mohanlan leo Jason
 *
 */
@Controller
@RequestMapping("/tasks")
@MenuSetting("menu-advertisement")
public class TaskController {

	@Autowired
	private TaskService taskService;

	/**
	 * 
	 * list all tasks
	 * 
	 * @return
	 */
	@RequestMapping("")
	@MenuSetting("submenu-task-list")
	public ModelAndView list(@RequestParam(value = "status", required = false) TaskStatus status, Page page, HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("task/task-list");

		int count = taskService.count();
		modelAndView.addObject("count", count);
		modelAndView.addObject("status", status);

		if (count > 0) {
			List<Task> tasks = taskService.list(status, page);
			modelAndView.addObject("tasks", tasks);
		}
		
		// handle query string
		String queryString = request.getQueryString();
		if (!StringUtils.isEmpty(queryString)) {
        	
        	Map<String, String> queries = StringUtils.parseQueryString(queryString);
            if(queries.containsKey("page")) {
                queries.remove("page");
            }

    		modelAndView.addObject("queryStatus", StringUtils.toQueryString(queries));
		}


		return modelAndView;
	}

	/**
	 * display the search panel
	 * 
	 * @return
	 */
	@RequestMapping("/search")
	public String search() {
		return "task/task-search";
	}

	/**
	 * do search
	 * 
	 * @return
	 */
	@RequestMapping(value = "/search", method = RequestMethod.GET, params = "filter")
	@MenuSetting("submenu-task-list")
	public ModelAndView onSearch(Page page, HttpServletRequest request,
			@RequestParam(value = "title", required = false) String adTitle,
			@RequestParam(value = "customerName", required = false) String customerName,
			@RequestParam(value = "socialType", required = false) SocialType socialType,
			@RequestParam(value = "taskStatus", required = false) TaskStatus taskStatus,
			@RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
			@RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {

		ModelAndView modelAndView = new ModelAndView("task/task-list");

		Filter filter = new Filter().addLike("advertisement.adTitle", adTitle)
									.addLike("customer.profile.nickname", customerName)
									.addEquals("advertisement.socialType", socialType)
									.addEquals("taskStatus", taskStatus).addEquals("startDate", startDate)
									.addEquals("endDate", endDate);

		int count = taskService.count(filter);
		modelAndView.addObject("count", count);

		if (count > 0) {
			List<Task> tasks = taskService.list(Task.class, filter, page);
			modelAndView.addObject("tasks", tasks);
		}

		// handle query string
		String queryString = request.getQueryString();
		Map<String, String> queries = StringUtils.parseQueryString(queryString);
		if (queries.containsKey("page")) {
			queries.remove("page");
		}

		modelAndView.addObject("queryString", StringUtils.toQueryString(queries));

		return modelAndView;
	}

	/**
	 * view the task detail
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/{id}")
	@MenuSetting("submenu-task-list")
	public ModelAndView view(@PathVariable("id") Integer id) {
		ModelAndView modelAndView = new ModelAndView("task/task-view");

		Task task = taskService.load(Task.class, id);
		modelAndView.addObject("task", task);

		return modelAndView;
	}

}
